package com.dgut.hisystem.controller;

import com.dgut.hisystem.entity.Doctor;
import com.dgut.hisystem.entity.Patient;
import com.dgut.hisystem.entity.User;
import com.dgut.hisystem.entity.VO.UserVO;
import com.dgut.hisystem.service.DoctorService;
import com.dgut.hisystem.service.PatientService;
import com.dgut.hisystem.service.UserService;
import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private PatientService patientService;
    @Resource
    private DoctorService doctorService;

    @RequestMapping("/search")
    List<User> searchUser() {
        return userService.search();
    }

    @RequestMapping("/getAllDoctor")
    List<Doctor> getAllDoctor() {
        return doctorService.getAllDoctors();
    }

    @RequestMapping("/get")
    User getUserById(long id) {
        return userService.getUserById(id);
    }

    @RequestMapping("/login")
    public String login(String username, String password){
        User u = userService.getUserByPhone(Long.parseLong(username));
        if (!password.equals(u.getYh_password())) {
            return "密码错误";
        }

        if(u.getYh_type() == 0){
            //管理员
            return "0";
        }else if(u.getYh_type() == 1){
            //患者
            return "1";
        }else if(u.getYh_type() == 2){
            //医生
            return "2";
        }else if(u.getYh_type() == 3){
            //收费员
            return "3";
        }else if(u.getYh_type() == 4){
            //药库管理员
            return "4";
        }

        return "error";
    }

    @RequestMapping("/insert")
    User insert(@RequestBody UserVO userVO) {
        try {
            User user = new User();
            user.setYh_phone(Long.parseLong(userVO.getPhone()));
            user.setYh_idCard(userVO.getId_number());
            String role = userVO.getRole();
            int type = 0;
            switch (role) {
                case "管理员":
                    type = 0;
                    break;
                case "病人": {
                    type = 1;
                    break;
                }
                case "医生": {
                    type = 2;
                    break;
                }
                case "费用管理员":
                    type = 3;
                    break;
                case "药库管理员":
                    type = 4;
                    break;
                default:
                    throw new ValueException("用户角色类型错误");
            }
            user.setYh_type(type);
            user.setYh_name(userVO.getUsername());
            user.setYh_email(null);
            user.setYh_password("123456");
            if (userVO.getSex().equals("女")) {
                user.setYh_sex(0);
            } else {
                user.setYh_sex(1);
            }
            user.setYh_address(userVO.getLocation());

            userService.insert(user);

            if (type == 1) {
                Patient patient = new Patient();
                patient.setYh_id(user.getYh_id());
                patient.setBr_sex(user.getYh_sex());
                patient.setBr_age(0);
                patient.setBr_name(user.getYh_name());
                patient.setBr_address(user.getYh_address());
                patientService.insert(patient);
            } else if (type == 2) {
                Doctor doctor = new Doctor();
                doctor.setYh_id(user.getYh_id());
                doctor.setYs_name(user.getYh_name());
                doctor.setYs_sex(user.getYh_sex());
                doctor.setYs_age(0);
                doctorService.insert(doctor);
            }


            return user;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/update")
    void update(@RequestBody User user) {
        userService.update(user);
    }

    @RequestMapping("/delete")
    void delete(String idCard) {
        userService.deleteByIdCard(idCard);
    }

}
